/*
 * Copyright 2013 Aliyun.com All right reserved. This software is the
 * confidential and proprietary information of Aliyun.com (\"Confidential
 * Information\"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Aliyun.com .
 */
package com.mtauto.service;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.mtauto.model.View;

/**
 * @author <a href=\"mailto:li.jinl@alibaba-inc.com\">Stone.J</a> Nov 25, 2013
 */
public class ViewService {

    private static final Logger                 LOGGER  = LoggerFactory.getLogger(ViewService.class);
    private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();

    private AdbService                          adbService;

    public View getView(String serialno) {
        String resp = adbService.uiautomator(serialno);
        if (resp == null) {
            return null;
        }
        try {
            DocumentBuilder build = FACTORY.newDocumentBuilder();
            Document doc = build.parse(new ByteArrayInputStream(resp.getBytes()));
            Node node = doc.getChildNodes().item(0).getChildNodes().item(0);
            View root = new View();
            parse(root, null, node);
            return root;
        } catch (Exception e) {
            LOGGER.error("parse view error.", e);
            return null;
        }
    }

    private void parse(View root, View parent, Node node) {
        if (!node.getNodeName().equals("node")) {
            return;
        }
        View view = (parent == null) ? root : new View();
        view.setType(node.getAttributes().getNamedItem("class").getNodeValue());
        view.setPkg(node.getAttributes().getNamedItem("package").getNodeValue());
        view.setText(node.getAttributes().getNamedItem("text").getNodeValue());
        view.setIndex(Integer.valueOf(node.getAttributes().getNamedItem("index").getNodeValue()));
        if (parent != null) {
            parent.getChildren().add(view);
        }
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            parse(root, view, node.getChildNodes().item(i));
        }

    }

    public void setAdbService(AdbService adbService) {
        this.adbService = adbService;
    }

    public static void main(String[] args) {
        AdbService adbService = new AdbService() {
            @Override
            public String uiautomator(String serialno) {
                if (serialno == null) {
                    return "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><hierarchy rotation=\"0\"><node index=\"0\" text=\"\" class=\"android.widget.FrameLayout\" package=\"android\" content-desc=\"\" checkable=\"false\" checked=\"false\" clickable=\"false\" enabled=\"true\" focusable=\"false\" focused=\"false\" scrollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,0][720,1280]\"><node index=\"0\" text=\"\" class=\"android.widget.FrameLayout\" package=\"android\" content-desc=\"\" checkable=\"false\" checked=\"false\" clickable=\"false\" enabled=\"true\" focusable=\"true\" focused=\"false\" scrollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,0][720,1280]\"><node index=\"0\" text=\"\" class=\"android.widget.FrameLayout\" package=\"android\" content-desc=\"\" checkable=\"false\" checked=\"false\" clickable=\"false\" enabled=\"true\" focusable=\"true\" focused=\"true\" scrollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,0][720,1280]\"><node index=\"0\" text=\"\" class=\"android.widget.FrameLayout\" package=\"android\" content-desc=\"\" checkable=\"false\" checked=\"false\" clickable=\"false\" enabled=\"true\" focusable=\"false\" focused=\"false\" scrollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,0][720,1280]\"><node index=\"0\" text=\"\" class=\"android.view.View\" package=\"android\" content-desc=\"\" checkable=\"false\" checked=\"false\" clickable=\"false\" enabled=\"true\" focusable=\"true\" focused=\"false\" scrollable=\"false\" long-clickable=\"false\" password=\"false\" selected=\"false\" bounds=\"[0,0][720,1280]\" /></node></node></node></node></hierarchy>";
                }
                return super.uiautomator(serialno);
            }
        };
        adbService.setAdb("D:/android/adt-bundle-windows-x86/sdk/platform-tools/adb.exe");

        ViewService viewService = new ViewService();
        viewService.setAdbService(adbService);

        try {
            System.out.println(viewService.getView(null).dump());
            System.out.println(viewService.getView("4df7d3731f82cf0b").dump());
        } finally {
            adbService.destroy();
        }

    }
}
